package com.sinosoft.perfect.system._authorization.action;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TRoleSystem;
import com.sinosoft.perfect.system._.po.TUserRolesysRelation;
import com.sinosoft.perfect.system._authorization.service.IUserRolesysAuthorizeService;
import com.sinosoft.perfect.system._roleSystem.service.IRoleSystemService;

@SuppressWarnings("serial")
public class UserRolesysBindingAction extends BaseAction {
	
	private IUserRolesysAuthorizeService iuserRolesysService;
	
	private IRoleSystemService roleSystemService;
	
	private TRoleSystem trolesystem;
	
	private TUserRolesysRelation tuserrolesysrelation;
	
	private String unitId;
	
	private String userId;
	
	private String type; // 单位页查询or用户页查询
	
	public String roleSystemQuery() {
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(trolesystem);
			request.setAttribute("roleSystems", roleSystems);
			request.setAttribute("unitId", unitId);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "unit".equals(type) ? roleSystemUnit() : roleSystemUser();
	}
	
	public String roleSystemUnit() {
		try {
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(trolesystem != null ? trolesystem : new TRoleSystem());
			request.setAttribute("roleSystems", roleSystems);
			request.setAttribute("unitId", unitId);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "user_rolesystem_bindunit";
	}
	
	public String roleSystemUser() {
		try {
			List<TUserRolesysRelation> userRoleSystems = iuserRolesysService.queryRolesystemsOfUser(userId);
			StringBuffer userRoleSystemIds = new StringBuffer();
			if (userRoleSystems != null && userRoleSystems.size() != 0) {
				for (int i = 0; i < userRoleSystems.size(); i++) {
					userRoleSystemIds.append(userRoleSystems.get(i).getRoleSystemId()).append(",");
				}
			}
			request.setAttribute("userRoleSystemIds", userRoleSystemIds.toString());
			
			List<TRoleSystem> roleSystems = roleSystemService.selectByTRoleSystem(trolesystem != null ? trolesystem : new TRoleSystem());
			StringBuffer roleSystemIds = new StringBuffer();
			if (roleSystems != null && roleSystems.size() != 0) {
				for (int i = 0; i < roleSystems.size(); i++) {
					roleSystemIds.append(roleSystems.get(i).getRoleSystemId()).append(",");
				}
			}
			request.setAttribute("roleSystemIds", roleSystemIds.toString());
			request.setAttribute("roleSystems", roleSystems);
			
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "user_rolesystem_binduser";
	}
	
	/**
	 * 
	 */
	
	public IUserRolesysAuthorizeService getIuserRolesysService() {
		return iuserRolesysService;
	}
	
	public void setIuserRolesysService(IUserRolesysAuthorizeService iuserRolesysService) {
		this.iuserRolesysService = iuserRolesysService;
	}
	
	public IRoleSystemService getRoleSystemService() {
		return roleSystemService;
	}
	
	public void setRoleSystemService(IRoleSystemService roleSystemService) {
		this.roleSystemService = roleSystemService;
	}
	
	/**
	 * 
	 */
	
	public TRoleSystem getTrolesystem() {
		return trolesystem;
	}
	
	public void setTrolesystem(TRoleSystem trolesystem) {
		this.trolesystem = trolesystem;
	}
	
	public TUserRolesysRelation getTuserrolesysrelation() {
		return tuserrolesysrelation;
	}
	
	public void setTuserrolesysrelation(TUserRolesysRelation tuserrolesysrelation) {
		this.tuserrolesysrelation = tuserrolesysrelation;
	}
	
	public String getUnitId() {
		return unitId;
	}
	
	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}
	
	public String getUserId() {
		return userId;
	}
	
	public void setUserId(String userId) {
		this.userId = userId;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
}
